package com.syswin.lhztest.util;

import com.csvreader.CsvReader;
import com.syswin.lhztest.db.model.MailEnpity;
import com.syswin.policy.Mail;
import com.syswin.policy.PolicyFilter;
import com.syswin.policy.PolicyFilterResult;
import com.syswin.policy.csv.PolicyReader;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

/**
 * @author:lhz
 * @date:2019/1/25 11:27
 */
public class CSVUtil {


	private static class SingletonHolder {

		private static final CSVUtil INSTANCE = new CSVUtil();

	}

	private CSVUtil() {
	}

	public static final CSVUtil getInstance() {

		return SingletonHolder.INSTANCE;

	}

	List<String[]> list = PolicyReader.getList();

	public static String baseePath = "D:/mailcomp/testdata/";
	public static String replaceStr = "/data/MailBackup/";
	List<String[]> rowList = new ArrayList<String[]>();

	public List<MailEnpity> read(int rowset) throws Exception {
		List<MailEnpity> mailEnpityList = new ArrayList<MailEnpity>();
		String filePath = baseePath + "csv/邮件记录(放行邮件).csv";
//		String filePath = baseePath + "csv/邮件记录(拦截邮件).csv";
//		String filePath = baseePath + "csv/邮件记录(威胁邮件 (含病毒邮件)).csv";
		CsvReader csvReader = null;
		try (
						InputStream is = new FileInputStream(filePath);

		) {

//			Charset cs = Charset.forName("gbk");
			// 创建CSV读对象

			csvReader = new CsvReader(is, ',', Charset.forName("utf-8"));
			if (rowList.size() == 0) {
				// 读表头
				csvReader.readHeaders();
				while (csvReader.readRecord()) {
					// 读一整行
//				System.out.println(csvReader.getRawRecord());
					rowList.add(csvReader.getValues());
				}
			}
			int datalin = ((rowset + 1) * 80);
			if (datalin >= rowList.size()) {
				datalin = rowList.size();
			}
			for (int row = rowset * 80; row < datalin; row++) {
				String[] params = rowList.get(row);
				if (params.length > 0) {
//					for (int i = 0; i < length; i++) {
					MailEnpity mailEnpity = new MailEnpity();

					mailEnpity.setCreatetime(params[0]);
					mailEnpity.setSendmail(params[1]);
					mailEnpity.setTomail(params[2]);
					mailEnpity.setTitle(params[3]);
					System.out.println(mailEnpity.getTitle());
					mailEnpity.setRightverify(params[4]);
					mailEnpity.setKeytype(params[5]);
					mailEnpity.setAttachname(params[6]);
					mailEnpity.setMailsize(Integer.parseInt(params[7]));
					mailEnpity.setSendip(params[8]);
					mailEnpity.setDealsatus(params[9]);
					mailEnpity.setDescripe(params[10]);
					mailEnpity.setBackupname(params[11]);
					try {
						String pathUrl = baseePath + (mailEnpity.getBackupname().replace(replaceStr, ""));
						Mail mail = MailUtil.getInstance().parserEmlFile(pathUrl);
						if (mail != null) {
							mailEnpity.setContent(mail.getContent());
							PolicyFilterResult result = new PolicyFilter().check(mail);
							mailEnpity.setPass(result.got ? 1 : 0);
							mailEnpity.setPriority(result.priority);
							mailEnpity.setPassinfo(result.desc);
						}
					} catch (Exception e) {
						mailEnpity.setContent("无法解析");
					}
					mailEnpityList.add(mailEnpity);

				}
			}


		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			csvReader.close();
		}
		return mailEnpityList;
	}

	public static void main(String[] args) throws Exception {
		long start = System.currentTimeMillis();
		CSVUtil.getInstance().read(1);
		long end = System.currentTimeMillis();
		System.out.println("time：" + (end - start));
		System.err.println("size：" + CSVUtil.getInstance().rowList.size());

		boolean a = false;
		boolean b =true;
		System.out.println("a &!b----"+(a^!b^true));
		System.out.println("a?!b:b----"+(a?!b:b));
	}



}


